package com.booksmanager.utils;

import com.booksmanager.pojo.User;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @ Author     ：Yan
 * @ Date       ：Created in 22:10 2020/5/18
 * @ Description：简单封装Session会话管理
 * @ Modified By：
 * @Version: $
 */
public class RequestSession<T> {
    //session  request
    private HttpSession httpSession;
    private HttpServletRequest httpServletRequest;

    public RequestSession() {
        httpServletRequest = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        httpSession = httpServletRequest.getSession();
        httpSession.setMaxInactiveInterval(30 * 60);
    }

    public void setSession(String key, Object object) {
        httpSession.setAttribute(key, object);
    }

    public T getSession(String key) {
        return (T) httpSession.getAttribute(key);
    }

    public String getSessionId() {
        return httpSession.getId();
    }

    public User getUser() {
        return (User) httpSession.getAttribute("user");
    }

    public void setUser(User user) {
        httpSession.setAttribute("user", user);
    }
}

